home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue72 / misc / nielsen / Test / sample / Custom / Sql / users.sql < prev   
Encoding:
Text File  |  2002-08-14  |  2.3 KB  |  87 lines

  1. select sql_users_insert();
  2. select sql_users_update(1,'Username1','Password1',1);
  3. select sql_users_insert();
  4. select sql_users_update(2,'Username2','Password2',1);
  5.  
  6. drop function sql_users_verify(text,text);
  7. CREATE FUNCTION sql_users_verify (text,text) RETURNS int4 AS '
  8. DECLARE
  9.     record1 record;  id int4 :=0;
  10.     username text; password text;
  11.     active int4 := 0;
  12. BEGIN
  13.    username := clean_text($1);
  14.    password := clean_text($2);
  15.  
  16.    FOR record1 IN SELECT users_id FROM users
  17.         where users.username = username
  18.           and users.password = password
  19.           and users.active = 1
  20.       LOOP
  21.       id := record1.users_id;
  22.    END LOOP;
  23.  
  24.      -- If id < 1 check if username exists .
  25.    IF id is NULL THEN  
  26.      FOR record1 IN SELECT users_id,active FROM users
  27.         where users.username = username
  28.        LOOP
  29.        id := record1.users_id;
  30. --       active := record1.active;
  31.          -- If active is < 1, isn not active.
  32. --       IF active < 1 THEN return (-3); END IF;
  33.          -- If id is > 0, password is incorrect.
  34.        IF id  > 0 THEN return (-2); END IF;
  35.        END LOOP;
  36.    END IF;
  37.  
  38.         -- If id is < 1, username does not exist. 
  39.    IF id  < 1 THEN return (-1); END IF;
  40.  
  41.      -- Everything has passed, return id as users_id.
  42.    return (id);
  43. END;
  44. ' LANGUAGE 'plpgsql';
  45. select sql_users_verify('Username1','Password1');
  46.  
  47. drop function sql_users_exists(text);
  48. CREATE FUNCTION sql_users_exists (text) RETURNS int4 AS '
  49. DECLARE
  50.     record1 record;  id int4 :=0;
  51.     username text; password text;
  52. BEGIN
  53.    username := clean_text($1);
  54.  
  55.    FOR record1 IN SELECT users_id FROM users
  56.         where users.username = username
  57.       LOOP
  58.       id := record1.users_id;
  59.    END LOOP;
  60.  
  61.      -- Everything has passed, return id as users_id.
  62.    return (id);
  63. END;
  64. ' LANGUAGE 'plpgsql';
  65. select sql_users_exists('Username1');
  66.  
  67. drop function sql_users_active(text);
  68. CREATE FUNCTION sql_users_active (text) RETURNS int4 AS '
  69. DECLARE
  70.     record1 record;  id int4 :=0;
  71.     username text; password text;
  72. BEGIN
  73.    username := clean_text($1);
  74.  
  75.    FOR record1 IN SELECT users_id FROM users where users.username = username
  76.       LOOP
  77.       id := record1.users_id;
  78.    END LOOP;
  79.  
  80.      -- Everything has passed, return id as users_id.
  81.    return (id);
  82. END;
  83. ' LANGUAGE 'plpgsql';
  84. select sql_users_active('Username1');
  85.  
  86.  
  87.